package com.eminem.service.impl;

import com.eminem.dao.ArticleDao;
import com.eminem.dao.FavoriteDao;
import com.eminem.model.Article;
import com.eminem.model.Favorite;
import com.eminem.service.ArticleService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleServletimpl implements ArticleService {

    private ArticleDao articleDao = new ArticleDao();

    private FavoriteDao favoriteDao = new FavoriteDao();

    @Override
    public List<Map<String, Object>> getArticleAll(String userId,Integer currentPage,String name) {
        return articleDao.getArticleAll(userId,currentPage,name);
    }

    @Override
    public long getArticleCount(String name) {
        return articleDao.getArticleCount(name);
    }

    @Override
    public Map<String, Object> getArticleList(String id) {
        return articleDao.getArticleList(id);
    }

    @Override
    public boolean AddArticle(Article article) {
        return articleDao.save(article) > 0;
    }

    @Override
    public List<Map<String, Object>> getCollectionArticle(Integer currentPage,String page,String name) {
        return articleDao.findArticle(currentPage,page,name);
    }

    @Override
    public long getCollectionCount(String userId,String name) {
        return articleDao.getCount(userId,name);
    }

    @Override
    public boolean CollectionArttcile(String userId, String id) {
        Favorite favorite = new Favorite();
        favorite.setUId(Integer.parseInt(userId));
        favorite.setAId(Integer.parseInt(id));
        return favoriteDao.save(favorite) > 0;
    }

    @Override
    public boolean DeleteConllctrionArticle(Integer id) {
        return favoriteDao.deleteById(id) > 0;
    }

    @Override
    public Favorite ArtucileCollection(String userId, String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("u_id",userId);
        map.put("a_id",id);
        List<Favorite> list = favoriteDao.findByCols("and", map);
        if (!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

}
